//
//  Line.m
//  D3J
//
//  Created by doductrung on 6/18/13.
//  Copyright (c) 2013 doductrung. All rights reserved.
//

#import "Line.h"
#import <math.h>
@implementation Line

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id) initWithPoints: (NSMutableArray*) points
{   self = [super init];
    self.backgroundColor = [UIColor clearColor];
    self.points = points;
    CGPoint startP = CGPointMake([self.points[0] floatValue], [self.points[1] floatValue]);
    CGPoint endP = CGPointMake([self.points[2] floatValue], [self.points[3] floatValue]);
    self.x = startP.x;
    self.y = startP.y;
    self.frame = CGRectMake(self.x, self.y, abs(endP.x - startP.x), abs(endP.y - startP.y));
    self.strokeColor = [UIColor blackColor].CGColor;
    self.strokeWidth = 10;
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, self.strokeColor);
    CGContextSetLineWidth(context, self.strokeWidth);
    CGPoint startP = CGPointMake([self.points[0] floatValue], [self.points[1] floatValue]);
    CGPoint endP = CGPointMake([self.points[2] floatValue], [self.points[3] floatValue]);
    NSLog(@"%f, %f, %f, %f ", startP.x, startP.y, endP.x, endP.y);
    CGContextMoveToPoint(context, 0, 0);
    CGContextAddLineToPoint(context, endP.x - startP.x, endP.y - startP.y);
    CGContextSetLineWidth(context, 1.0);
    CGRect r = CGRectMake(0,0, self.frame.size.width, self.frame.size.height);
    CGContextAddRect(context, r);
    CGContextStrokePath(context);
}


@end
